package com.example.project.projectspringcloud.common.alipay;

import com.example.project.projectspringcloud.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class Payment {

    /**
     * 创建支付链接
     * ==============================================================================
     *
     * @param paygateway
     * @param service
     * @param signType
     * @param outTradeNo
     * @param inputCharset
     * @param partner
     * @param key
     * @param showUrl
     * @param body
     * @param totalFee
     * @param paymentType
     * @param sellerEmail
     * @param subject
     * @param notifyUrl
     * @param return_url
     * @param paymethod
     * @param defaultbank
     * @param extraCommonParam
     * @return
     */
    public static String createUrl(String paygateway, String service,
                                   String signType, String outTradeNo, String inputCharset,
                                   String partner, String key, String showUrl, String body,
                                   String totalFee, String paymentType, String sellerEmail,
                                   String subject, String notifyUrl, String return_url,
                                   String paymethod, String defaultbank, String extraCommonParam) {

        Map<String, String> params = new HashMap<String, String>();
        params.put("service", service);
        params.put("partner", partner);
        params.put("subject", subject);
        params.put("body", body);
        params.put("out_trade_no", outTradeNo);
        params.put("total_fee", totalFee);
        params.put("show_url", showUrl);
        params.put("payment_type", paymentType);
        params.put("seller_email", sellerEmail);
        params.put("return_url", return_url);
        params.put("notify_url", notifyUrl);
        params.put("paymethod", paymethod);
        params.put("extra_common_param", extraCommonParam);
        if (StringUtils.isNotBlank(defaultbank)) {
            params.put("defaultbank", defaultbank);
        }
        params.put("_input_charset", inputCharset);
        String sign = StringUtil.md5(getContent(params, key));
        String parameter = "";
        parameter = String.valueOf(parameter) + paygateway;
        List<String> keys = new ArrayList<String>(params.keySet());
        for (int i = 0; i < keys.size(); i++) {
            try {
                parameter = String.valueOf(parameter) + keys.get(i) + "=" + URLEncoder.encode((String) params.get(keys.get(i)), inputCharset) + "&";
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        parameter = String.valueOf(parameter) + "sign=" + sign + "&sign_type=" + signType;
        return parameter;
    }

    private static String getContent(Map<String, String> params, String privateKey) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = (String) keys.get(i);
            String value = (String) params.get(key);
            if (i == keys.size() - 1) {
                prestr = String.valueOf(prestr) + key + "=" + value;
            } else {
                prestr = String.valueOf(prestr) + key + "=" + value + "&";
            }
        }
        return String.valueOf(prestr) + privateKey;
    }
}